/**
 *
 * 分页插件
 * @author sunyu 2014-09-23
 */
;(function(factory) {
	if (typeof define === 'function' && define.amd) {
		// AMD. Register as an anonymous module.
		define([ 'jquery.pager' ], factory);
	} else {
		// Browser globals
		factory(jQuery);
	}
}(function($) {
	$.fn.pager = function(recordCount, opts) {
		opts = jQuery.extend({
			pageSize : 10,
			currentPage : 1,
			spanInterval : 2,
			firstText : '第一页',
			preText : '前一页',
			nextText : '下一页',
			lastText : '最后一页',
			isShowTotal : true,
			isShowJump : true,
			callback : function() {
				return false
			}
		}, opts || {});
		return this.each(function() {
			var panel = jQuery(this);
			var pageCount = getPageCount();// obtain pages
			var intPageIndex = parseInt(opts.currentPage);//获取当前页
			if(pageCount==0){
				intPageIndex = 1;
			}
			var interval = parseInt(opts.spanInterval); // 设置间隔
			var start = getStartAndEnd()[0];// get start
			var end = getStartAndEnd()[1];// get end
			
			/**
			 * calculate start and end use to generate page numbers
			 */
			function getStartAndEnd(){
				var start = Math.max(1, intPageIndex - interval); 
				var end = Math.min(intPageIndex + interval, pageCount);
				if (intPageIndex < interval + 1) {
					end = (2 * interval + 1) > pageCount ? pageCount
							: (2 * interval + 1);
				}
				if ((intPageIndex + interval) > pageCount) {
					start = (pageCount - 2 * interval) < 1 ? 1
							: (pageCount - 2 * interval);
				}
				return [start, end];
			}
			// draw the links
			function drawLinks(){
				panel.empty();
				var first = '';
				if (intPageIndex == 1||pageCount==0) {
					first = "<span class='disabled'>"+opts.firstText+"</span>";
				} else {
					first = "<a href='javascript:void(0)' first='" + 1
							+ "' class='firstPage'>"+opts.firstText+"</a>";
				}
				// 添加上一页
				var pre = '';
				if (intPageIndex == 1||pageCount==0) {
					pre = "<span class='disabled'>"+opts.preText+"</span>";
				} else {
					pre = "<a href='javascript:void(0)' pre='" + (intPageIndex - 1)
							+ "' class='prePage'>"+opts.preText+"</a>";
				}
				// 生成页码
				var a = '';
				for ( var j = start; j < end + 1; j++) {
					if (j == intPageIndex) {
						a += "<span class='current'>" + j + "</span>";
					} else {
						a += "<a href='javascript:void(0)' class='link'>" + j + "</a>";
					} 
				} 
				// 添加下一页
				var next = '';
				if (intPageIndex == pageCount||pageCount==0) {
					next = "<span class='disabled'>"+opts.nextText+"</span>";
				} else {
					next = "<a href='javascript:void(0)' next='" + (intPageIndex + 1)
							+ "' class='nextPage'>"+opts.nextText+"</a>";
				}
				// 添加下一页
				var last = '';
				if (intPageIndex == pageCount||pageCount==0) {
					last = "<span class='disabled'>"+opts.lastText+"</span>";
				} else {
					last = "<a href='javascript:void(0)' last='" + pageCount
							+ "' class='lastPage'>"+opts.lastText+"</a>";
				}
				var totalText = '';
				//根据配置决定是否显示总记录和总页数
				if(opts.isShowTotal){
					totalText = "共" + recordCount + "条," + "共" + pageCount + "页";
				}
				var jumpText = '';
				//根据配置决定是否显示跳转框
				if(opts.isShowJump){
					if(pageCount==0){
						jumpText = "转到<input type='text' name ='pageIndex' value="+intPageIndex+" id='goInput'/>页<span class='disabled'>确定</span>";
					}else{
						jumpText = "转到<input type='text' name ='pageIndex' value="+intPageIndex+" id='goInput'/>页<a id='sureBtn' href='javascript:void(0)''>确定</a>";
					}
				}
				str = "&nbsp;" + first + pre + a + next + last + "&nbsp;" + jumpText
						+ "&nbsp;&nbsp;" + totalText;
				return str;
				
			}
			/**
			 * calculate total pages
			 */
			function getPageCount(){
				return Math.ceil((recordCount / opts.pageSize));
			}
			function pageClick(pageIndex) {
				opts.callback(pageIndex);
			}
			// When all initialisation is done, draw the links
			var text = drawLinks();
			panel.html(text);
			//鼠标单击事件
			$(".firstPage").click(function() {
				pageClick($(this).attr('first'));
				return false;
			});
			$(".prePage").click(function() {
				pageClick($(this).attr('pre'));
				return false;
			});
			$(".link").click(function() {
				pageClick($(this).text());
				return false;
			});
			$(".nextPage").click(function() {
				pageClick($(this).attr("next"));
				return false;
			});
			$(".lastPage").click(function() {
				pageClick($(this).attr("last"));
				return false;
			});
			$("#sureBtn").click(function() {
				var page = $("#sureBtn").prev().val();
				var reg = /^[1-9]\d*$/;
				if (reg.test(page)) {
					var num = parseInt(page);
					if (num < 0 || num > pageCount) {
						alert("请输入有效页面!");
					} else {
						pageClick(num);
						return false;
					}
				} else {
					alert("页号只能为正整数!");
				}
			});
		});
	}
}));
